package com.myapp.ruyixing.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.CityHotMenu;
import com.myapp.ruyixing.model.CityHotMenu.CityHotLabel;
import com.myapp.ruyixing.model.CityHotMenu.HotCell;
import com.myapp.ruyixing.ui.widget.TitleBar;

public class SearchCityMenuFragment extends Fragment implements OnClickListener {

	private ListView mCategoryListView;
	private MyListAdapter mListAdapter;
	private View mRootView;

	private List<CityHotLabel> mAllCatetoriesData;
	private List<CityHotMenu.HotCell> mCategoriesData;
	private Label currentLabel;
	private View currentHighlightLabelButton;

	private boolean isShowTitleBar = true;

	private OnCategoryItemClickListener mOnItemClickListener;

	private enum Label {
		FOOD, ENTERTAINMENT, HOTEL, SHOPPING, BUSINESS, TRAVEL, SCENE
	}

	public static interface OnCategoryItemClickListener {
		public void onItemClicked(HotCell cell);
	}

	private static Map<Label, String> sNameMappings = new HashMap<Label, String>();

	static {
		sNameMappings.put(Label.FOOD, "美食");
		sNameMappings.put(Label.HOTEL, "住宿");
		sNameMappings.put(Label.TRAVEL, "行");
		sNameMappings.put(Label.SCENE, "旅游");
		sNameMappings.put(Label.ENTERTAINMENT, "娱乐");
		sNameMappings.put(Label.SHOPPING, "购物");
		sNameMappings.put(Label.BUSINESS, "商务助理");
	}

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			CityHotMenu cityHotMenu = (CityHotMenu) o;

			mAllCatetoriesData.clear();
			mAllCatetoriesData.addAll(cityHotMenu.labels);
			setCurrentLabel(currentLabel);
		}

		@Override
		public void onFailure(Throwable throwable) {
			if (getActivity() != null) {
				Toast.makeText(getActivity().getApplicationContext(),
						"服务器异常，请稍后再试", Toast.LENGTH_SHORT).show();
			}
		}
	};

	public static SearchCityMenuFragment getInstance(boolean showTitleBar) {
		SearchCityMenuFragment fragment = new SearchCityMenuFragment();
		Bundle bundle = new Bundle();
		bundle.putBoolean("showTitleBar", showTitleBar);
		fragment.setArguments(bundle);

		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (getArguments() != null) {
			isShowTitleBar = getArguments().getBoolean("showTitleBar", true);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_search_city_menu, container,
				false);
	};

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {

		mAllCatetoriesData = new ArrayList<CityHotLabel>();
		mCategoriesData = new ArrayList<CityHotMenu.HotCell>();
		initViews(view);
		mRootView = view;

		mListAdapter = new MyListAdapter(mCategoriesData);
		mCategoryListView.setAdapter(mListAdapter);

		currentHighlightLabelButton = view.findViewById(R.id.food);
		setCurrentLabel(Label.FOOD);

		RYHttpClient.getCityHotCategory(getActivity(), mHttpResponseHandler);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		if (getActivity() instanceof OnCategoryItemClickListener) {
			mOnItemClickListener = (OnCategoryItemClickListener) getActivity();
		}
	}

	private void initViews(View view) {
		mCategoryListView = (ListView) view.findViewById(R.id.categories);

		view.findViewById(R.id.food).setOnClickListener(this);
		view.findViewById(R.id.entertainment).setOnClickListener(this);
		view.findViewById(R.id.hotel).setOnClickListener(this);
		view.findViewById(R.id.shoppoing).setOnClickListener(this);
		view.findViewById(R.id.business).setOnClickListener(this);
		view.findViewById(R.id.travel).setOnClickListener(this);
		view.findViewById(R.id.scene).setOnClickListener(this);

		TitleBar titleBar = (TitleBar) view.findViewById(R.id.title_bar);

		if (!isShowTitleBar) {
			titleBar.setVisibility(View.GONE);
		} else if (getActivity() instanceof MainActivity) {
			((TitleBar) view.findViewById(R.id.title_bar)).getIconView()
					.setVisibility(View.GONE);
			titleBar.setTitleText(getString(R.string.privilege_title));
		} else {
			titleBar.setOnIconClickedListener(this);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.food:
			if (currentLabel == Label.FOOD) {
				return;
			}
			currentHighlightLabelButton.setSelected(false);
			currentHighlightLabelButton = mRootView.findViewById(R.id.food);
			setCurrentLabel(Label.FOOD);

			break;
		case R.id.entertainment:
			if (currentLabel == Label.ENTERTAINMENT) {
				return;
			}
			currentHighlightLabelButton.setSelected(false);
			currentHighlightLabelButton = mRootView
					.findViewById(R.id.entertainment);
			setCurrentLabel(Label.ENTERTAINMENT);

			break;
		case R.id.hotel:
			if (currentLabel == Label.HOTEL) {
				return;
			}
			currentHighlightLabelButton.setSelected(false);
			currentHighlightLabelButton = mRootView.findViewById(R.id.hotel);
			setCurrentLabel(Label.HOTEL);

			break;
		case R.id.shoppoing:
			if (currentLabel == Label.SHOPPING) {
				return;
			}
			currentHighlightLabelButton.setSelected(false);
			currentHighlightLabelButton = mRootView
					.findViewById(R.id.shoppoing);
			setCurrentLabel(Label.SHOPPING);

			break;
		case R.id.business:
			if (currentLabel == Label.BUSINESS) {
				return;
			}
			currentHighlightLabelButton.setSelected(false);
			currentHighlightLabelButton = mRootView.findViewById(R.id.business);
			setCurrentLabel(Label.BUSINESS);

			break;
		case R.id.travel:
			if (currentLabel == Label.TRAVEL) {
				return;
			}
			currentHighlightLabelButton.setSelected(false);
			currentHighlightLabelButton = mRootView.findViewById(R.id.travel);
			setCurrentLabel(Label.TRAVEL);

			break;
		case R.id.scene:
			if (currentLabel == Label.SCENE) {
				return;
			}
			currentHighlightLabelButton.setSelected(false);
			currentHighlightLabelButton = mRootView.findViewById(R.id.scene);
			setCurrentLabel(Label.SCENE);

			break;

		case R.id.img_icon:
			if (!(getActivity() instanceof MainActivity)) {
				getActivity().finish();
			}
			break;

		default:
			break;
		}
	}

	private void setCurrentLabel(Label label) {
		currentLabel = label;
		currentHighlightLabelButton.setSelected(true);

		mCategoriesData.clear();
		String mappingName = sNameMappings.get(currentLabel);
		for (CityHotLabel item : mAllCatetoriesData) {
			if (mappingName.equals(item.label)) {
				mCategoriesData.addAll(item.cells);
			}
		}

		mListAdapter.notifyDataSetChanged();
	}

	private class MyListAdapter extends ArrayAdapter<CityHotMenu.HotCell> {
		public MyListAdapter(List<CityHotMenu.HotCell> data) {
			super(getActivity(), -1, data);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.cell_search_city_menu, parent, false);
			}

			((TextView) convertView).setText(getItem(position).name);
			((TextView) convertView).setTag(getItem(position));
			convertView.setOnClickListener(mCategoryViewClickListener);

			return convertView;
		}
	}

	private OnClickListener mCategoryViewClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			HotCell cell = (HotCell) v.getTag();
			if (cell != null && mOnItemClickListener != null) {
				mOnItemClickListener.onItemClicked(cell);
			}
		}
	};
}
